package cn.bb.bbstudy.controller;

import cn.bb.bbstudy.dao.InviteDAO;
import cn.bb.bbstudy.pojo.Invite;
import cn.bb.bbstudy.pojo.User;
import cn.bb.bbstudy.pojo.UserInfo;
import cn.bb.bbstudy.service.UserInfoService;
import cn.bb.bbstudy.service.UserService;
import cn.bb.bbstudy.utils.BBUtils;
import cn.bb.bbstudy.vo.UserForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private InviteDAO inviteDAO;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @GetMapping("index")
    public String index() {
        return "index";
    }

    @GetMapping("toLogin")
        public String toLogin() {
        return "login";
    }

    @GetMapping("register")
    public String register() {
        return "register";
    }

    @PostMapping("/register")
    public String register(UserForm userForm, Model model) {
        BBUtils.print(userForm);
        //判断用户是否已经存在
        if (userService.findUserByNme(userForm.getUsername()) != null) {
            model.addAttribute("regMsg", "用户名已存在");
            return "register";
        }
        //判断密码是否都相同
        if (!userForm.getPassword().equals(userForm.getRepassword())) {
            model.addAttribute("regMsg", "两次密码不相同,请重新输入");
            return "register";
        }
        //根据邀请码查库 看是否被使用过
        Invite invite = inviteDAO.findCode(userForm.getCode());
        if (invite == null) {
            model.addAttribute("regMsg", "该邀请码不存在");
            return "register";
        } else {
            //1表示被使用过
            if (invite.getStatus() == 1) {
                model.addAttribute("regMsg", "该邀请码不存在");
                return "register";
            } else {
                //接下来就可以注册用户了
                User user = new User();
                user.setUid(BBUtils.getUuid());
                //默认就是普通用户
                user.setRoleId(2);
                user.setUsername(userForm.getUsername());
                user.setPassword(passwordEncoder.encode(userForm.getPassword()));
                user.setGmtCreate(BBUtils.getTime());
                user.setLoginDate(BBUtils.getTime());
                //保存对象
                userService.insertSelective(user);
                System.out.println("注册成功");

                //userinfo需要设置uid
                UserInfo userInfo = new UserInfo();
                userInfo.setUid(user.getUid());
                userInfoService.insert(userInfo);

                //激活邀请码
                invite.setActiveTime(BBUtils.getTime());
                invite.setStatus(1);
                invite.setUid(user.getUid());
                inviteDAO.updateByPrimaryKeySelective(invite);
                return "redirect:/toLogin";
            }


        }

    }
}
